package com.sinoglobal.ningxia.widget;

import android.content.Context;
import android.os.Handler;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.LinearLayout;
import android.widget.ListView;

import com.sinoglobal.ningxia.R;
import com.sinoglobal.ningxia.activity.IBase;
import com.sinoglobal.ningxia.activity.amusement.FilterRequirement;
import com.sinoglobal.ningxia.adapter.ScreenListAdapter;
import com.sinoglobal.ningxia.adapter.ScreenListTwoAdapter;
import com.sinoglobal.ningxia.beans.AreaListVo;

/**
 * @Title: ScreenListView.java
 * @Package com.sinoglobal.ningxia.widget
 * @Description: TODO(popwindow 的View)
 * @author czz
 * @date 2014-7-2 上午9:50:16
 */
public class ScreenListView implements IBase, OnItemClickListener {

	private Context context;
	private ListView popwindowLeft;
	private ListView popwindowRight;
	private ScreenListAdapter screenlist;
	private ScreenListTwoAdapter screentwolist;
	private AreaListVo result;
	private int type; // 1 按区域 2 按分类
	private LinearLayout popwindowView;
	private Handler handler;
	public static final int POPNUM1 = 5;
	public static final int POPNUM2 = 6;
	public static final int POPNUMALL = 7;

	public ScreenListView(Context context, AreaListVo result, int type, Handler handler) {
		this.context = context;
		this.result = result;
		this.type = type;
		this.handler = handler;
	}

	public View loadView() {
		popwindowView = (LinearLayout) LayoutInflater.from(context).inflate(R.layout.activity_popwindow_lv, null);
		init();
		return popwindowView;
	}

	@Override
	public void init() {
		popwindowLeft = (ListView) popwindowView.findViewById(R.id.popwindow_left);
		popwindowRight = (ListView) popwindowView.findViewById(R.id.popwindow_lv_right);
		popwindowLeft.setOnItemClickListener(this);
		popwindowRight.setOnItemClickListener(this);

		// 计算popWindow的宽高
		screenlist = new ScreenListAdapter(context);
		screentwolist = new ScreenListTwoAdapter(context);
		if (type == 1) {
			if (result.getQuyulist() != null) {
				screenlist.setAreaListVo(result.getQuyulist());
				screentwolist.setAreaVo(result.getQuyulist().get(0).getQuyu());
				popwindowRight.setAdapter(screentwolist);
				popwindowRight.setVisibility(View.GONE);
			}
		} else {
			screenlist.num = 0;
			screenlist.setAreaListVo(result.getTypelist());
			popwindowRight.setVisibility(View.GONE);
		}
		popwindowLeft.setAdapter(screenlist);
	}

	@Override
	public void showListener() {

		popwindowRight.setVisibility(View.VISIBLE);
	}

	@Override
	public void onItemClick(AdapterView<?> arg0, View arg1, int arg2, long arg3) {
		switch (arg0.getId()) {
		case R.id.popwindow_left:
			if (screenlist.num != arg2) { // 如果当前选中的是该项 则点击没效果

				if (arg2 != 0) {
					if (type == 1) { // 按区域划分会有二级选择显示 若按分类划分二级选择 不显示
						popwindowRight.setVisibility(View.VISIBLE);
					}
					screenlist.num = arg2;
					screentwolist.oldfatherNum = arg2;
					if (type == 1) {
						screentwolist.setAreaVo(screenlist.getAreaListVo().get(arg2 - 1).getQuyu());
					} else {
						FilterRequirement.classid = screenlist.getItem(arg2 - 1).getClassid();
						handler.sendEmptyMessage(POPNUM1);
					}
					screenlist.notifyDataSetChanged();
					screentwolist.notifyDataSetChanged();
				} else {
					FilterRequirement.quyu = "";
					FilterRequirement.classid = "";
					screenlist.num = arg2;
					if (type == 1) { // 按区域划分会有二级选择显示 若按分类划分二级选择 不显示
						popwindowRight.setVisibility(View.GONE);
					}
					handler.sendEmptyMessage(POPNUMALL);
					screenlist.notifyDataSetChanged();
				}
			}
			break;
		case R.id.popwindow_lv_right:
			screentwolist.num = arg2;
			FilterRequirement.quyu = screentwolist.getItem(arg2).getClassid();
			screentwolist.fatherNum = screenlist.num;
			screentwolist.notifyDataSetChanged();
			handler.sendEmptyMessage(POPNUM2);
			break;
		default:
			break;
		}
	}
}
